﻿namespace Liger.Web.UI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Liger.Web.Common;
    using Liger.Web.Common.Extensions;
    using System.ComponentModel;
    using Liger.Web.Javascript;
    using Liger.Web.Common.JSON;
    
    public class Form : UIComponentBase
    {

        public Form()
            : this(new StringBuilder())
        {

        }
        public Form(StringBuilder writer)
            : base(writer)
        {
            this.Fields = new List<FormField>();
            this.Properties = new Dictionary<string, object>();
        }
        //控件宽度
        public int? InputWidth { get; set; }

        //标签宽度
        public int? LabelWidth { get; set; }

         //间隔宽度
        public int? Space { get; set; }

         
        public string RightToken { get; set; }

        //标签对齐方式
        public string LabelAlign { get; set; }

        //控件对齐方式
        public string Align{ get; set; }

        //字段
        public List<FormField> Fields{ get; set; }

        //创建的表单元素是否附加ID
        public bool? AppendID{ get; set; }

        //生成表单元素ID的前缀
        public string PrefixID { get; set; }

        //json解析函数
        public Func<string> ToJSON { get; set; }

        /// <summary>
        /// 其他可扩展的属性
        /// </summary>
        public Dictionary<string, object> Properties
        {
            get;
            private set;
        }

        /// <summary>
        /// 返回ligerForm的Manager
        /// </summary>
        public string Manager
        {
            get;
            set;
        }

        public override void WriteHTML()
        {
            if (!ID.HasValue())
                ID = "form" + System.Guid.NewGuid().ToString();

            string TagName = string.IsNullOrEmpty(this.TagName) ? "form" : this.TagName;


            if (TagName.ToLower() == "form")
            {
                Writer.Append("<{1} id='{0}' method='post'></{1}>".FormatWith(ID, TagName));
            }
            else
            {
                Writer.Append("<{1} id='{0}'></{1}>".FormatWith(ID, TagName));
            }
            base.WriteScript();
        }
        public override void WriteScript()
        {
            if (base.LoadInDocumentReady)
            {
                JSWriter.WriteJQueryOnLoad();
            }
            if (this.Manager.HasValue())
            {
                JSWriter.Append("window.{0} = ".FormatWith(this.Manager));
            }
            JSWriter.WriteJQueryPlugin(ID, "ligerForm");
            JSWriter.Append(GetFormParmJSON());
            JSWriter.WriteEndJQueryPlugin();
            if (base.LoadInDocumentReady)
            {
                JSWriter.WriteEndJQueryOnLoad();
            }
            base.WriteScript();
        }

        private string GetFieldsJSON(List<FormField> fields)
        {
            IJavascriptObjectWriter jswfields = new JavascriptObjectWriter();
            jswfields.PropertyVariableCase = JSWriter.PropertyVariableCase;
            jswfields.WriteArray();
            for (int i = 0; fields != null && i < fields.Count; i++)
            {
                var field = fields[i];
                jswfields.WriteOjbect()
                 .WriteProperty("Name", field.Name)
                 .WriteProperty("Label", string.IsNullOrEmpty(field.Label) ? field.Name : field.Label)
                 .WriteProperty("Type", field.Type)
                 .WriteProperty("Newline", field.Newline)
                 .WriteProperty("Group", field.Group)
                 .WriteProperty("Groupicon", field.Groupicon)
                 .WriteProperty("LabelWidth", field.LabelWidth)
                 .WriteProperty("LabelAlign", field.LabelAlign)
                 .WriteProperty("Align", field.Align)
                 .WriteProperty("Space", field.Space)
                 .WriteProperty("ComboboxName", field.ComboboxName);

                if (field.Attr != null && field.Attr.Keys.Count > 0)
                {
                    jswfields.WriteProperty("Attr", () => GetObjectJSON(field.Attr));
                }

                if (field.Validate != null && field.Validate.Keys.Count > 0)
                {
                    jswfields.WriteProperty("Validate", () => GetObjectJSON(field.Validate));
                }

                foreach (var prop in field.Properties.Keys)
                {
                    var value = field.Properties[prop];
                    jswfields.WritePropertyObject(prop, value);
                }

                jswfields.WriteEndOjbect();
            }
            jswfields.WriteEndArray();
            return jswfields.Output();
        }


        public string GetFormParmJSON()
        {
            IJavascriptObjectWriter JSWriter = new JavascriptObjectWriter();
            JSWriter.PropertyVariableCase = this.JSWriter.PropertyVariableCase;
            JSWriter.WriteOjbect();

            JSWriter.WriteProperty("InputWidth", InputWidth)
                .WriteProperty("LabelWidth", LabelWidth)
                .WriteProperty("Space", Space)
                .WriteProperty("RightToken", RightToken)
                .WriteProperty("LabelAlign", LabelAlign)
                .WriteProperty("AppendID", AppendID)
                .WriteProperty("PrefixID", PrefixID)
                .WriteProperty("ToJSON", ToJSON);

            JSWriter.WriteProperty("Fields", () => GetFieldsJSON(Fields));

            foreach (var prop in this.Properties.Keys)
            {
                var value = this.Properties[prop];
                JSWriter.WritePropertyObject(prop, value);
            }

            foreach (var even in this.Events)
            {
                JSWriter.WriteProperty(even.Key, even.Value);
            }

            JSWriter.WriteEndOjbect();

            return JSWriter.Output();
        }
    }
}
